// pages/patrol_list/patrol_list.js
var app = getApp();
var util = require('../../utils/util.js');
var applet_park_api_url = app.globalData.applet_park_api_url;
var applet_park_api_url_imgUrl = app.globalData.applet_park_api_url_imgUrl;
const {
  requestDataparams
} = require('../../utils/request.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: applet_park_api_url_imgUrl,
    currenttab: 0,
    leader_confirmed: '0',
    statuses: 2,
    currenttDate: '',
    lists: [],
    title: '',
    isLeader: '',
    role:'',
    roleId:'',
    status:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    

  },
  timePro() {
    this.data.currenttDate = this.getBefOrAft(0, this.data.currenttDate);
    this.setData(this.data);
    this.data.lists = [];
    this.setData(this.data)
    this.loadData();
  },
  timeNaxt() {
    this.data.currenttDate = this.getBefOrAft(1, this.data.currenttDate);
    this.data.lists = [];
    this.setData(this.data)
    this.loadData();
  },
  // 获取前天或者后一天  type: 0: 前一天 1：后一天
  getBefOrAft(type, date) {
    let forMatDate = date.replace(/-/g, '/') // 格式化日期 2019-02-23 => 2019/02/23
    let d = new Date(forMatDate) // new Date() 不能识别 2019-01-02  需转化为 2019/01/02
    if (type === 0) {
      d = +d - 1000 * 60 * 60 * 24
    } else if (type === 1) {
      d = +d + 1000 * 60 * 60 * 24 // d前面需要加上 +
    }
    d = new Date(d)
    return this.dateFormat(d) // 格式化 将日期格式转化为我们需要展示格式
  },

  // 格式化日期，传入日期对象   =>2019-01-11  
  dateFormat(date) {
    let y = date.getFullYear()
    let m = date.getMonth() + 1
    let d = date.getDate()
    if (m <= 9) {
      m = '0' + m
    }
    if (d <= 9) {
      d = '0' + d
    }
    return y + '-' + m + '-' + d
  },
  loadData() {
    this.setData({
      title: '数据正在加载',
    })
    this.setData({
      lists: []
    })
    var that = this;
    var id =app.globalData.id || wx.getStorageSync("id");
    requestDataparams('/api/checkTasks/queryMyTask/'+id, {status:that.data.currenttab}, 'GET','json').then(res => {
      if (res.data.code == 0) {
        if(res.data.data.code == 3){
          this.setData({
            title: '暂无权限',
          })
        }else{
          var lists = res.data.data.list;
          if (lists.length == 0) {
            that.setData({
              title: '暂无更多数据',
            })
          }else{
            that.setData({
              title: '',
            })
          }
          
          for (var i = 0; i < res.data.data.list.length;i++){
            res.data.data.list[i].expectedStartTime = res.data.data.list[i].expectedStartTime.slice(5, 16)
            res.data.data.list[i].expectedEndTime = res.data.data.list[i].expectedEndTime.slice(5, 16)
          }
          that.setData({
            lists: res.data.data.list,
            role: res.data.data.role,
            roleId: res.data.data.roleId,
          })
          that.setData(that.Date)
        }
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        });
      }
    }).catch(err => {
      // 
    });
  },
  enterStaffdetail(e) {//员工详情  员工修改
    // 
    var that = this;
    var item = e.currentTarget.dataset.item;
    var type = e.currentTarget.dataset.type;
    var id = e.currentTarget.dataset.id;
    var taskName = e.currentTarget.dataset.taskname;
    
    if(type == 'modify'){//修改状态
      item.status = 2;//未完成
      // 
      this.modifyState(item)
    }else{//查看详情

    }
      wx.navigateTo({//员工
        url: '../patrol_record/patrol_record?id='+id+'&taskName='+taskName+'&roleId='+that.data.roleId,
      })
  },
  modifyState(item){//员工修改巡检状态
    // 
    requestDataparams('/api/checkTasks/updateTask', {taskId:item.id,status:item.status,remark:''}, 'PUT','json').then(res => {
      if (res.data.code == 0) {
        
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        });
      }
    }).catch(err => {
      // 
    });
  },
  enterDirectordetail(e){//主管确认  查看详情
    var that = this;
    var item = e.currentTarget.dataset.item;
    var type = e.currentTarget.dataset.type;
    var id = e.currentTarget.dataset.id;
    var taskName = e.currentTarget.dataset.taskname;
    var status = e.currentTarget.dataset.status;
    // if(type == 'modify'){//修改状态
    //   item.status = 4;//未完成
    //   
    //   this.modifyState(item)
    // }else{//查看详情

    // }
    wx.navigateTo({//主管
      url: '../place_list/place_list?id='+id+'&taskName='+taskName+'&roleId='+that.data.roleId+'&status='+status+'&isLeader=true',
    })
  },
  confirm(e) {
    var type = e.currentTarget.dataset.type;
    if (type == 1) {
      wx.navigateTo({
        url: '../confirm2/confirm2?checkTaskId=' + e.currentTarget.dataset.id + '&name=' + e.currentTarget.dataset.name + '&et=' + e.currentTarget.dataset.et + '&st=' + e.currentTarget.dataset.st + '&id=' + e.currentTarget.dataset.id + '&st=' + e.currentTarget.dataset.st + '&isLeader=' + e.currentTarget.dataset.isleader,
      })
    }

    if (type === 2) {
      wx.navigateTo({
        url: '../confirm/confirm?checkTaskId=' + e.currentTarget.dataset.id + '&name=' + e.currentTarget.dataset.name + '&et=' + e.currentTarget.dataset.et + '&st=' + e.currentTarget.dataset.st + '&id=' + e.currentTarget.dataset.id + '&isLeader=' + e.currentTarget.dataset.isleader,
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    var date = new Date()
    var year = date.getFullYear()
    var month = ((date.getMonth() + 1) < 10 ? ("0" + (date.getMonth() + 1)) : date.getMonth() + 1);
    var day = (date.getDate() < 10 ? ("0" + date.getDate()) : date.getDate());
    var hour = date.getHours()
    var minute = date.getMinutes()
    var second = date.getSeconds()
    this.data.currenttDate = year + '-' + month + '-' + day;
    var isLeader = wx.getStorageSync('isLeader');
    this.data.isLeader = isLeader;
    this.setData(this.data);
    this.loadData();
  },
  toAsssaff(e) {
    if (e.currentTarget.dataset.stus == 1 || e.currentTarget.dataset.stus == 2) {

      wx.navigateTo({
        url: '../assign_detail/assign_detail?name=' + e.currentTarget.dataset.name + '&et=' + e.currentTarget.dataset.et + '&st=' + e.currentTarget.dataset.st + '&id=' + e.currentTarget.dataset.id + "&tid=" + e.currentTarget.dataset.tid + "&groupId=" + e.currentTarget.dataset.groupid,
      })
    }


  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    wx.stopPullDownRefresh();
    this.loadData();
    wx.showToast({
      title: '刷新成功',
      icon: 'success',
      duration: 2000
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  packBp(e) {
    this.setData({
      currenttab: e.currentTarget.dataset.index
    })
    if (e.currentTarget.dataset.index == 0) {
      this.setData({
        statuses: '2',
        leader_confirmed: '0',
      })
    }
    if (e.currentTarget.dataset.index == 1) {
      this.setData({
        statuses: '3,7,9',
        leader_confirmed: '0',
      })
    }
    if (e.currentTarget.dataset.index == 2) {
      this.setData({
        statuses: '4,8',
        leader_confirmed: 1,
      })
    }

    // 
    // 
    this.loadData();
  },

})